package com.atguigu.srb.core.controller.api;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.util.RegexValidateUtils;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserIndexVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author tjf
 * @since 2022-01-03
 */
@Api(tags = "会员接口")
@RestController
//@CrossOrigin
@Slf4j
@RequestMapping("/api/core/userInfo")
public class UserInfoController {

    private final RedisTemplate<String, Object> redisTemplate;
    private final UserInfoService userInfoService;

    public UserInfoController(RedisTemplate<String, Object> redisTemplate, UserInfoService userInfoService) {
        this.redisTemplate = redisTemplate;
        this.userInfoService = userInfoService;
    }

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO) {
        // 验证手机号和验证码和密码不为空
        Assert.notEmpty(registerVO.getMobile(), ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(registerVO.getCode(), ResponseEnum.CODE_NULL_ERROR);
        Assert.notEmpty(registerVO.getPassword(), ResponseEnum.PASSWORD_NULL_ERROR);
        // 验证手机号正确，redis中有值
        Assert.isTrue(RegexValidateUtils.checkCellphone(registerVO.getMobile()), ResponseEnum.MOBILE_ERROR);
        String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:code:" + registerVO.getMobile());
        Assert.notNull(codeGen, ResponseEnum.CODE_ERROR);
        // 验证验证码正确
        Assert.equals(registerVO.getCode(), codeGen, ResponseEnum.CODE_ERROR);
        // 调用服务
        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        // 验证手机号和密码不为空
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        String ip = request.getRemoteAddr();
        UserInfoVO userInfoVO = userInfoService.login(loginVO, ip);
        return R.ok().data("userInfo", userInfoVO);
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(@ApiParam("令牌") @RequestHeader( required = false) String token) {
        boolean result = JwtUtils.checkToken(token);
        if (result) {
            return R.ok();
        } else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }

    @ApiOperation("校验手机号是否存在")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@ApiParam("手机号") @PathVariable String mobile) {
        return userInfoService.checkMobile(mobile);
    }

    @ApiOperation("获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public R getIndexUserInfo(@RequestHeader String token) {
        Long userId = JwtUtils.getUserId(token);
        UserIndexVO userIndexVO = userInfoService.getIndexUserInfo(userId);
        return R.ok().data("userIndexVO", userIndexVO);
    }
}

